package org.nanhill.support.jmx.dwjmxservice;

import java.io.IOException;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import com.sun.jdmk.TraceManager;
import com.sun.jdmk.comm.HtmlAdaptorServer;

public class BaseStdAgent {


    public BaseStdAgent() {

    }

    public  void startAgent(Object inMBean) {

	try {
	    TraceManager.parseTraceProperties();
	}
	catch (IOException e) {
	    e.printStackTrace();
	}

	System.out.println("Creating the MBeanServer....");
	MBeanServer server = MBeanServerFactory.createMBeanServer();

      try {
        ObjectName tpMBeanName = new ObjectName("MBean:name=ClickMeter");
        server.registerMBean(inMBean, tpMBeanName);
      } catch (Exception e) {
         System.out.println("Cannot register ClickMeter MBean!");
         e.printStackTrace();
         return;
      }

	System.out.println("Creating an HTML protocol adaptor..");
	HtmlAdaptorServer hadaptor = new HtmlAdaptorServer();
	ObjectName adaptorName = null;
	try {
	    adaptorName = new ObjectName("Adaptor:name=hadaptor,port=8082");
	    server.registerMBean(hadaptor, adaptorName);
	} catch(Exception e) {
	    System.out.println("Cannot create the HTML protocol adaptor!");
	    e.printStackTrace();
	    return;
	}
	hadaptor.start();
    }

}